package com.teddy.view.tree;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TaxonomyTreeItem extends SemanticTreeItem{
	private String label;
	private Resource res;
	private ResIterator subclasses;

	public TaxonomyTreeItem(Tree modelTreeItem, int style) {
		super(modelTreeItem, style);
	}
	
	public TaxonomyTreeItem(TreeItem modelTreeItem, int style, Resource res) {
		super(modelTreeItem, style);
		this.res = (Resource) res;
		subclasses = res.getModel().listResourcesWithProperty(RDFS.subClassOf, res);	
		
		setText(res.getURI().substring(res.getURI().indexOf("#")+1));
		label = getText();
		setItemCount(1);
	}

	@Override
	public void expand() {
		this.removeAll();
		while(subclasses.hasNext()){
			Resource n = subclasses.next();
			TaxonomyTreeItem item = new TaxonomyTreeItem(this, SWT.NONE, n);
		}
		subclasses = res.getModel().listResourcesWithProperty(RDFS.subClassOf, res);
	}
	
	public String getLiteralProperty(Property property){
		if (res.hasProperty(property))
			res.getProperty(property).getString();
		return "";
	}
	
	public void setLiteralSingleProperty(Property property, String o){
		if (res.hasProperty(property))
			res.removeAll(property);
		res.addLiteral(property, o);
	}
	
	public String getInverseLiteralProperty(Property property){
		ResIterator r =res.getModel().listResourcesWithProperty(property, res);
		
		while(r.hasNext()){
			if (property.isResource())
				return r.next().getURI();
			if (property.isLiteral())
				return r.next().toString();
		}
		return null;
	}
	
	

	@Override
	public void refresh() {
	}

	@Override
	public void remove() {
		res.getModel().remove(res.listProperties());
	}

	@Override
	public void rename() {
	}

	public String getPrefLAbel(){
		return getLabel();
	}
	
	public void setPrefLAbel(String label){
		setLabel(label);
		
	}
	public void setLabel(String label) {
		this.label = label;
	}

	public String getLabel() {
		return label;
	}
	
	public void addLiteralProperty(Property p, String o){
		res.addProperty(p, o, selectedLanguage);
	}
}
